package o2o.web.actions.order;

import com.jinyou.utils.common.AjaxResult;
import com.jinyou.utils.common.ValidateUtil;
import o2o.cache.agent.AgentOrderOverTimeMemory;
import o2o.cache.agent.ShopAgentRelationMemory;
import o2o.cache.order.OrderGoodsMemory;
import o2o.cache.order.OrderInfoMemory;
import o2o.cache.shop.ShopMemory;
import o2o.cache.user.UserInfoMemory;
import o2o.cache.user.UserOnlineImpl;
import o2o.dao.PO.agent.OrderOverTimeInfo;
import o2o.dao.PO.order.*;
import o2o.company.model.ShopInfo;
import o2o.dao.PO.user.UserInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashSet;


//订单
@Controller
public class SubmitOrderAction {


    //获取商品是否购买过
    @RequestMapping(method = RequestMethod.POST, value = "/user/goods/hasBuyCount")
    @ResponseBody
    public String order_goods_can_buy(String token, Long goodsId, Integer isToday) {
        //验证用户有效性
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }
        if (ValidateUtil.isNotID(goodsId)) {
            return AjaxResult.successResult(0);
        }

        if (null == isToday) {//是否今天
            isToday = 1;
        }
        boolean isTodayBoolean = false;
        if (0 == 1 - isToday) {
            isTodayBoolean = true;
        }
        HashSet<String> orderSet = OrderGoodsMemory.getInstance().orderNoListOfGoodsId(goodsId);
        if (null == orderSet || (null != orderSet && orderSet.size() < 1)) {
            return AjaxResult.successResult(0 + "");
        }
        Integer count = OrderInfoMemory.getInstance().hasOrderCountInOrderNoSet(user.getUsername(), orderSet, isTodayBoolean);
        return AjaxResult.successResult(count + "");
    }

    //-------------------------管理员


    // 提交订单获取配送费
    @RequestMapping("/user/submit/order/delivery/money_oth")
    @ResponseBody
    public String getDeliverMoney_dada(String sysAppKey, Long shopId, Long addressId, String ver) {
        double deliveryPrice = 0;
        if (StringUtils.isNotEmpty(ver) && ver.equals("1.2")) {         // 计算配送费的生成方式
            OrderOverTimeInfo orderDeliveryInfo = AgentOrderOverTimeMemory.getInstance().getAgentOrderOverTimeInfoByAgentId(ShopAgentRelationMemory.getInstance().getAgentByShopId(shopId));
            if (orderDeliveryInfo == null || (StringUtils.isEmpty(orderDeliveryInfo.getDeliveryType())) || orderDeliveryInfo.getDeliveryType().equals("sys")) {
                StringBuffer buffer = new StringBuffer("{\"status\":1,\"type\":\"local\"");
                return buffer.toString();
            }
        }
        try {
            deliveryPrice = SubmitOrderMan.getDaDaPostDeliveryPrice(sysAppKey, addressId, shopId);
        } catch (Exception e) {
            return AjaxResult.errorResult(e.getMessage());
        }
        return AjaxResult.successResult(deliveryPrice + "");
    }
//
//    @RequestMapping("/auto/shop")
//    @ResponseBody
//    public String auto_shop(String sysAppKey, Long shopId, Long addressId) {
//        ShopService.getInstance().registerOrUpdateDeliverShop(sysAppKey, ShopMemory.getInstance().getShopById(shopId));
//        return AjaxResult.successResult();
//    }

//    @RequestMapping("/auto/order")
//    @ResponseBody
//    public String auto_shop(String sysAppKey, String orderNo) {
//        OrderInfo info = OrderInfoMemory.getInstance().getOrderInfo(orderNo);
//        if (info == null) {
//            return AjaxResult.errorResult("sss");
//        }
//        ShopOrderService.getInstance().sendDeliverOrderData(sysAppKey, info);
//        return AjaxResult.successResult();
//    }

    // 靠谱送 店铺下跑腿订单问题
    @RequestMapping("/shop/user/order/submit")
    @ResponseBody
    public String shopOrderSubmit(String token, String buyName, String buyPhone,
                                  String note,
                                  Double totalWeight,
                                  Double goodsPrice,
                                  Double buyLat, Double buyLng,
                                  String buyProvince, String buyCity,
                                  String buyCounty, String buyAddress,
                                  String buyAddress2, String buyAddress3) {
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }
        ShopInfo shop = ShopMemory.getInstance().getShopByUsername(user.getUsername());
        if (shop == null) {
            return AjaxResult.errorResult("未获取到店铺信息");
        }
        if (ValidateUtil.isNull(buyName) || ValidateUtil.isNull(buyPhone)) {
            return AjaxResult.errorResult("用户信息不能为空");
        }
        if (ValidateUtil.isNull(buyCity)) {
            return AjaxResult.errorResult("城市不能为空");
        }
        OrderSubmitResult result = SubmitOrderMan.shopSubmitOrder(shop, buyName, buyPhone,
                totalWeight, goodsPrice,
                buyLat, buyLng,
                buyProvince, buyCity,
                buyCounty, buyAddress,
                buyAddress2, buyAddress3, note);
        if (null != result && result.isSuccess()) {
            return AjaxResult.successResult(result);
        } else {
            return AjaxResult.errorResult(result.getError());
        }
    }
}
